package com.phoxell;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageFrame{
	private static String NEWLINE="\n";
	public static int getParamInt(HttpServletRequest req,String arg){
		String p=req.getParameter(arg);
		return p==null?0:Integer.parseInt(p);
	}

	public String aside="";
	public String contentType="text/html;charset=utf-8";
	public String footer;
	public String head;
	public String menu="<ul id=menu><li><a href=/>首页</a>"+
			"<li><a href=/products/>产品</a>"+
			"<li><a href=/downloads/>下载</a>"+
			"<li><a href=/community/>社区</a></ul>";
	private StringBuilder out=new StringBuilder();
	private HttpServletResponse resp;
	public Object title;
	public String tools;

	public PageFrame(HttpServletResponse rsp)throws IOException{
		resp=rsp;
	}

	public void begin(){
	}
	public void end()throws IOException{
		resp.setContentType(contentType);
		PrintWriter w=resp.getWriter();
		if(contentType.equals("text/html;charset=utf-8")){
			w.print("<!doctype html><html>"+
					"<head>"+
					"<meta charset=utf-8>"+
					"<link rel=stylesheet type=text/css href=/css/>"+
					"<title>"+title+"</title>"+
					"</head>"+
					"<script type=text/javascript src=/js/iehelper.js>" +
					"</script><body><nav>");
			if(tools!=null)
				w.print(tools);
			w.print(menu);
			w.print("</nav><article");
			if(aside==null)
				w.print(" class=full");
			w.print("><h1>"+title+"</h1>");
			w.print(out.toString());
			w.print("</article>");
			if(aside!=null){
				w.print("<aside>");
				w.print(aside);
				w.print("</aside>");
			}
			w.print("<footer><ul>"+
					"<li><a href=/about/contact.html>联系我们</a>"+
					"<li><a href=/about/sitemap.html>网站地图</a>"+
					"</ul>&copy 杭州孚石电子有限公司</footer></body>"+
					"<script src=/js/adxon.js></script>"+
					"<script src=/js/ctrl.js></script>"+
					"<script src=/js/></script></html>");
		}else w.print(out.toString());
	}
	public void print(long value){
		out.append(value);
	}
	public void print(Object value){
		out.append(value);
	}
	public void println(){
		out.append(NEWLINE);
	}
	public void println(long value){
		out.append(value);
		out.append(NEWLINE);
	}
	public void println(Object value){
		out.append(value);
		out.append(NEWLINE);
	}
	public void printNz(long value){
		if(value!=0)
			out.append(value);
	}
	public void sendRedirect(String arg)throws IOException{
		resp.sendRedirect(arg);
	}
}